package com.zt.bus.assistant.service;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zt.bus.assistant.inter.InstructionMapper;
import com.zt.bus.assistant.model.Instruction;
import com.zt.servlet.ReqBean;
import com.zt.system.model.BusinessException;
import com.zt.system.model.SqlBuilder;
import com.zt.system.service.BaseService;
import com.zt.util.DateUtil;
import com.zt.util.StringUtil;

import java.util.Date;

@Service
public class InstructionService extends BaseService {

    @Autowired
    private InstructionMapper instructionMapper;

    /**
     * 查询
     *
     * @param reqBean
     * @return
     */
    public JSONObject findInstruction(ReqBean reqBean) {
        SqlBuilder sqlBuilder = new SqlBuilder("select *  from DZ_INSTRUCTION n where 1=1", new Object[0]);
        sqlBuilder.addWhereLike2("n.TITLE", reqBean.getInfo("title"));
        sqlBuilder.orderBy("order by n.INSTRUCTION_ORDER");
        return this.queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
    }

    /**
     * 新增
     *
     * @param reqBean
     */
    public void addInstruction(ReqBean reqBean) {
    	Instruction instruction = getInstruction(reqBean);
    	instruction.setId(intIncrementId("DZ_INSTRUCTION", "ID"));// 手动设置Id
    	instruction.setMakeTime(instruction.getModifyTime());
        instructionMapper.insert(instruction);
    }

    /**
     * 修改
     *
     * @param reqBean
     */
    public void updateInstruction(ReqBean reqBean) {
        String id = reqBean.getInfo("id");
        if (StringUtil.isEmpty(id)) {
            throw new BusinessException("获取主键失败");
        }
        Instruction instruction = getInstruction(reqBean);
        instructionMapper.updateByPrimaryKeySelective(instruction);
    }

    /**
     * 删除
     *
     * @param reqBean
     */
    public void removeInstruction(ReqBean reqBean) {
        String id = reqBean.getInfo("id");
        if (StringUtil.isEmpty(id)) {
            throw new BusinessException("获取主键失败");
        }
        instructionMapper.deleteByPrimaryKey(id);
    }

    /**
     * 从表单获取对象
     *
     * @param reqBean
     * @return
     */
    private Instruction getInstruction(ReqBean reqBean) {
    	Instruction instruction = new Instruction();

    	instruction.setId(reqBean.getInfo("id"));
    	instruction.setTitle(reqBean.getInfo("title"));
    	instruction.setContent(reqBean.getInfo("content"));
    	instruction.setAndroidContent(reqBean.getInfo("androidContent"));
    	instruction.setInstructionOrder(reqBean.getInfoInt("instructionOrder"));
    	instruction.setType(reqBean.getInfoInt("type"));
    	instruction.setOperator(reqBean.getUserId());
    	instruction.setMakeTime(DateUtil.parseDateTime(reqBean.getInfo("makeTime")));
    	instruction.setModifyTime(new Date());
    	instruction.setRemark(reqBean.getInfo("remark"));

        return instruction;
    }

    public JSONArray query4Mobile(ReqBean reqBean) {
        String sql = "select *  from DZ_INSTRUCTION order by INSTRUCTION_ORDER ASC ";
        return query(sql);
    }
}